package com.atqar.base;

//线程 操作 资源


import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

//创建一个资源类
class zhiyuan{

    int a =50;
/*    public synchronized void  method(){
        if(a>0){
                               //获取线程的名字
            System.out.println(Thread.currentThread().getName()+"售卖了第"+(a--)+"张票,还剩下"+a+"张票");

            try {
                //睡眠两秒  TimeUnit枚举类有纳秒微秒毫秒 秒分时天
                TimeUnit.SECONDS.sleep(2);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }

        }
    }*/


    private final Lock lock = new ReentrantLock();/*ReentrantLock是可重入锁, 默认是悲观锁 ,默认是非公平锁效率优先,
                                                           fair=true是公平锁公平锁就是线程执行负载均衡轮询的方式不注重效率,公平和非公平锁本质都是AQS*/
    public void method(){
        lock.lock();
        try {
            if(a>0) {
                System.out.println(Thread.currentThread().getName() + "售卖了第" + (a--) + "张票,还剩下" + a + "张票");
            }
        }finally {
            //释放锁,不会造成阻塞
            lock.unlock();
        }
    }



}

public class SaleTicketDemo {
    public static void main(String[] args) {
        zhiyuan zy = new zhiyuan();

/*        //版本1  匿名内部类创建线程
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 50; i++) {
                    zy.method();
                }
            }
        },"a").start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 50; i++) {
                    zy.method();
                }
            }
        },"b").start();


        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 50; i++) {
                    zy.method();
                }
            }
        },"c").start();*/


        //用线程池完成三个人买票
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(3,
                5, 1L,TimeUnit.SECONDS,new ArrayBlockingQueue<>(45), Executors.defaultThreadFactory(),new ThreadPoolExecutor.DiscardOldestPolicy());

        try {
            for (int i = 1; i <= 50; i++) {
                threadPoolExecutor.execute(() -> {
                    zy.method();
                });
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            threadPoolExecutor.shutdown();
        }



//
//        new Thread(()-> {for (int i = 0; i < 50; i++) zy.method();},"a").start();
//        new Thread(()-> {for (int i = 0; i < 50; i++) zy.method();},"b").start();
//        new Thread(()-> {for (int i = 0; i < 50; i++) zy.method();},"c").start();



/*

/*
        原子整形类 AtomicInteger(不加synchronized情况下保证数据安全)



        //先获取数在做加法
        AtomicInteger atomicInteger = new AtomicInteger(5);
        System.out.println(atomicInteger.getAndIncrement());//5
        System.out.println(atomicInteger.get());//6
        //先获取数在做减法
        System.out.println(atomicInteger.getAndDecrement());//6
        System.out.println(atomicInteger.get());//5
*/


    }
}
